package it.getta.vicinometro.utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;

import com.google.android.maps.MapView;

public class ControlledMapView extends MapView implements OnGestureListener{
	
	private OnZoomLevelChangedListener mZoomLevelChangedListener;
	private OnAttachedToWindowListener mOnAttachedToWindowListener;
	private GestureDetector gd;    
    private OnSingleTapListener singleTapListener;
    private Context mContext; 
    private LongClickManager mLongClickManager;
    
    public interface LongClickManager {
    	public void onLongClickDetected(Point p);
    }

	public ControlledMapView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		setupGestures();
		
	}

	public ControlledMapView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		setupGestures();
	}

	public ControlledMapView(Context context, String apiKey) {
		super(context, apiKey);
		mContext = context;
		setupGestures();
	}
	
	private void setupGestures() {
//		setBuiltInZoomControls(true);
		setLongClickable(true);
    	gd = new GestureDetector(mContext, this);
//    	gd.setIsLongpressEnabled(true);
        
        //set the on Double tap listener  
        gd.setOnDoubleTapListener(new OnDoubleTapListener() {

			@Override
			public boolean onSingleTapConfirmed(MotionEvent e) {
				if (singleTapListener != null) {
					return singleTapListener.onSingleTap(e);
				} else {
					return false;
				}
			}

			@Override
			public boolean onDoubleTap(MotionEvent e) {
				ControlledMapView.this.getController().zoomInFixing((int) e.getX(), (int) e.getY());
				return false;
			}

			@Override
			public boolean onDoubleTapEvent(MotionEvent e) {
				return false;
			}
        	
        });
    }
	
	public void setOnSingleTapListener(OnSingleTapListener singleTapListener) {
		this.singleTapListener = singleTapListener;
	}

	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		if (mZoomLevelChangedListener != null) {
			mZoomLevelChangedListener.zoomLevelChanged(getZoomLevel());
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if (this.gd.onTouchEvent(ev)) {
			return true;
		} else {
			return super.onTouchEvent(ev);
		}
	}
	
	public interface OnZoomLevelChangedListener {
		public void zoomLevelChanged(int zoomLevel);
	}
	
	public interface OnAttachedToWindowListener {
		public void onAttach();
	}
	
	public void setOnAttachedToWindowListener(OnAttachedToWindowListener listener) {
		mOnAttachedToWindowListener = listener;
	}
	
	public void setOnZoomLevelChangedListener(OnZoomLevelChangedListener listener) {
		mZoomLevelChangedListener = listener;
	}
	
	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		if(mOnAttachedToWindowListener != null) {
			mOnAttachedToWindowListener.onAttach();
		}
	}

	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
//		TODO
		Log.d("TAG", "onLongPress");
		if(mLongClickManager != null) {
			mLongClickManager.onLongClickDetected(new Point((int)e.getX(), (int)e.getY()));
		}
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		return false;
	}

	public LongClickManager getLongClickManager() {
		return mLongClickManager;
	}

	public void setLongClickManager(LongClickManager mLongClickManager) {
		this.mLongClickManager = mLongClickManager;
	}
	
}
